/*
 *  multiProA.c
 *  
 *
 *  Created by Wang Zhuchen on 2/13/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "syscall.h"

void wait(int num) {
	int i;
	for (i=0; i<num; i++) {
		Yield();
	}
}

void abc() {
	int i;
	for (i=0; i<10; i++) {
		Write("*", 1, ConsoleOutput);
		Yield();
	}
	Exit(0);
}

void bcd() {
	int i;
	Fork(abc);
	for (i=0; i<20; i++) {
		Write("@", 1, ConsoleOutput);
		Yield();
	}
	Exit(0);
}

/* fork contains fork */
void fork_test() {
	int i;
	Fork(bcd);
	for (i=0; i<30; i++) {
		Write(".", 1, ConsoleOutput);
		Yield();
	}
	Write("\n\n", 2, ConsoleOutput);
}

/* exec test */
void exec_test() {	/* tesk ok */
	int i;
	Exec("../test/multiProB", 17);
	for (i=0; i<30; i++) {
		Write(".", 1, ConsoleOutput);
		Yield();
	}
	Write("\n\n", 2, ConsoleOutput);
}

void exec() {
	int i;
	Exec("../test/multiProB", 17);
	for (i=0; i<30; i++) {
		Write(".", 1, ConsoleOutput);
		Yield();
	}
	Exit(0);
}

/* fork contains exec */
void fork_contains_exec() { /* test ok */
	int i;
	Fork(exec);
	for (i=0; i<30; i++) {
		Write("*", 1, ConsoleOutput);
		Yield();
	}
	Write("\n\n", 2, ConsoleOutput);
}

void exec_contains_fork() { /* test ok */
	int i;
	Exec("../test/multiProA", 17);
	for (i=0; i<30; i++) {
		Write(".", 1, ConsoleOutput);
		Yield();
	}
	Write("\n\n", 2, ConsoleOutput);
}

int main() {
	Write("fork test...\n", 13, ConsoleOutput);
	fork_test();
	wait(35);
	Write("exec test...\n", 13, ConsoleOutput);
	exec_test();
	wait(35);
	Write("fork contains exec test...\n", 27, ConsoleOutput);
	fork_contains_exec();
	wait(35);
	Write("exec contains fork test...\n", 27, ConsoleOutput);
	exec_contains_fork();
	return 0;
}

